package com.sinodata.bsm.cicp.collector.was6;

import java.util.ArrayList;
import java.util.List;

import org.apache.log4j.Logger;
import org.dom4j.Document;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;
import org.dom4j.Node;

import com.sinodata.bsm.cicp.exception.CollectorException;
import com.sinodata.bsm.cicp.finder.was6.Util;
import com.sinodata.bsm.cicp.util.CollectorUtil;
import com.sinodata.bsm.common.vo.ResPropertyValue;

/**
 * 
 * <p>
 * Description: 
 * </p>
 *
 * @author liulibo
 * @version 1.0

 * <p>
 * History: 
 *
 * Date                     Author         Version     Description
 * ---------------------------------------------------------------------------------
 * 2012-12-26 AM 10:02:33   liulibo        1.0         To create
 * </p>
 *
 * @since 
 * @see
 */
public class ServletSessionCollector extends WasCollector {

    private static final Logger logger = Logger.getLogger(ServletSessionCollector.class);
    private static final String MODULE = "servletSessionsModule";

    @Override
    public List<ResPropertyValue> collect(String ip) throws CollectorException {
        String nodeName = this.paramMap.get("NodeName");
        String serverName = this.paramMap.get("ServerName");
        String port = this.getParamMap().get("ServerPort");
        List<ResPropertyValue> value = new ArrayList<ResPropertyValue>();
        String[] propMetas = getPropMeta();
        try {
            String respXml = WasValueBuffer.getInstance().getXml(ip, port, MODULE);
            if (respXml == null) {
                return null;
            }
            Document doc = DocumentHelper.parseText(respXml);
            Element root = doc.getRootElement();
            Node node = root.selectSingleNode("Node[@name='" + nodeName + "']");
            Node server = node.selectSingleNode("Server[@name='" + serverName + "']");
            List<Element> data = server.selectNodes("Stat/Stat");
            for (Element cp : data) {
                if (!this.paramMap.get("ServletSessionName").equals(cp.attributeValue("name"))) {
                    continue;
                }
                ResPropertyValue propertyValueX = new ResPropertyValue();
                ResPropertyValue.PropValue[] proValues = new ResPropertyValue.PropValue[getPropMeta().length];
                Node e1 = cp.selectSingleNode("RangeStatistic[@ID='7']");
                String LiveCount = DocumentHelper.parseText(e1.asXML()).getRootElement().attributeValue("value");
                proValues[0] = ResPropertyValue.createPropValue(LiveCount, propMetas[0]);
                propertyValueX.setValues(proValues);
                value.add(propertyValueX);

            }
        } catch (Exception e) {
            logger.error("failed to collect by ServletSessionCollector", e);
            throw new CollectorException(e);
        }
        return value;
    }

    @Override
    public String[] getParamMeta() {
        //        return new String[] { "服务器端口", "节点名", "服务器名", "会话管理器名" };
        return new String[] { "ServerPort", "NodeName", "ServerName", "ServletSessionName" };
    }

    @Override
    public String[] getPropMeta() {
        //        return new String[] { "当前会话数" };
        //return new String[] { "CurrentSessionsNum" };
        return new String[] { "$1" };
    }

}
